package com.majie.algorithm.linked_list;

import com.majie.algorithm.common.List;
import org.w3c.dom.Node;

public class SingleLinkedList<E> implements List<E> {

    private int size;

    private Node<E> first;

    private static class Node<E> {
        E element;

        Node<E> next;

        Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }
    }

    @Override
    public void clear( ) {
        size = 0;
        first = null;
    }

    @Override
    public int size( ) {
        return size;
    }

    @Override
    public boolean isEmpty( ) {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) >= 0;
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    @Override
    public E get(int index) {
        Node<E> node = node(index);
        return node.element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E old = node.element;
        node.element = element;
        return old;
    }

    @Override
    public void add(int index, E element) {
//        if (index == 0) {
//            this.first = new Node<>(element, null);
//        } else {
//            Node<E> prev = node(index - 1);
//            prev.next = new Node<>(element, null);
//
//        }

        Node node = first;
        if (node == null) {
            first = new Node<>(element, null);
        } else {
            node = node(index);
            Node tempNext = node.next;
            node.next = new Node<>(element, null);

            if (tempNext != null) {
                node.next.next = tempNext;
            }
        }
        size++;
    }

    /**
     * 获取index处节点的对象
     */
    private Node<E> node(int index) {
        rangeCheckForAdd(index);
        int count = 0;
        Node<E> node = first;
        while (node.next != null && count < index) {
            node = node.next;
            count++;
        }
        return node;
    }

    @Override
    public E remove(int index) {
        rangeCheckForAdd(index);
        Node<E> node = first;
        if (index == 0) {
            this.first = first.next;
        } else {
            Node<E> prev = node(index - 1);
            node = prev.next;
            prev.next = prev.next.next;
        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        int count = 0;
        Node<E> node = first;
        while (node != null) {
            if (node.element.equals(element)) {
                return count;
            }
            node = node.next;
            count++;
        }
        return -1;
    }

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }

    private void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
    }


    @Override
    public String toString( ) {
        StringBuilder sb = new StringBuilder();
        Node node = first;
        sb.append("[");
        while (node != null) {
            sb.append(node.element).append(",");
            node = node.next;
        }
        sb.append("]");
        return sb.toString();
    }
}
